package com.atguigu.lease.common.converter;

import com.atguigu.lease.model.enums.ItemType;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;
import org.springframework.stereotype.Component;
/*
*
 * Created by 小可乐$ on 2024/8/12$ 16:18$
 * description:枚举类转换器到mysql里的插入数据
 */


@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String s) {
//        获取全部枚举类属性
//        方式一：ItemType.class.
        ItemType[] values = ItemType.values();
        for (ItemType value : values) {
            if (value.getCode().equals(Integer.parseInt(s))) {
                return value;
            }
        }
        throw  new IllegalArgumentException("非法参数异常");
    }

    @Override
    public JavaType getInputType(TypeFactory typeFactory) {
        return null;
    }

    @Override
    public JavaType getOutputType(TypeFactory typeFactory) {
        return null;
    }
}
